local wanzhen = fk.CreateSkill{
  name = "rfenghou_3d13j__wanzhen",
}

local U = require "packages.utility.utility"

wanzhen:addEffect("viewas", {
  prompt = function(self, player)
    return player.chained and "#rfenghou_3d13j__wanzhen-viewas2:::"..player:getMark("@rfenghou_3d13j__wanzhen") or "#rfenghou_3d13j__wanzhen-viewas1"
  end,
  pattern = ".|.|.|.|.|basic",
  interaction = function(self, player)
    local all_names = {}
    all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames("rfenghou_3d13j__wanzhen", all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = wanzhen.name
    return card
  end,
  before_use = function(self, player)
    local room = player.room
    local num = player:getMark("@rfenghou_3d13j__wanzhen")
    local trueinvoke = false
      if not player.chained then
        trueinvoke = true
        player:setChainState(true)
        if not player.dead then
          room:setPlayerMark(player, "@rfenghou_3d13j__wanzhen", 3)
        end
      elseif player.chained then
        player:setChainState(false)
        if not player.dead then
          if num > 0 then
            room:askToDiscard(player, {
              min_num = num,
              max_num = num,
              include_equip = true,
              skill_name = wanzhen.name,
              cancelable = false
            })
            room:setPlayerMark(player, "@rfenghou_3d13j__wanzhen", 0)
          else
            room:setPlayerMark(player, "@rfenghou_3d13j__wanzhen", 0)
          end
        end
      end
    if not trueinvoke then return "" end
    end,
  enabled_at_play = function(self, player)
    local can = table.filter(player.player_cards[Player.Hand], function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
    return not player.chained or (player.chained and #can >= player:getMark("@rfenghou_3d13j__wanzhen"))
  end,
  enabled_at_response = function(self, player, res)
    local can = table.filter(player.player_cards[Player.Hand], function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
    return not player.chained or (player.chained and #can >= player:getMark("@rfenghou_3d13j__wanzhen"))
  end,
})

wanzhen:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@rfenghou_3d13j__wanzhen") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:removePlayerMark(player, "@rfenghou_3d13j__wanzhen", 1)
  end,
})

wanzhen:addEffect(fk.ChainStateChanged, {
  can_refresh = function (self, event, target, player, data)
    return target == player and not player.chained
  end,
  on_refresh = function (self, event, target, player, data)
      player.room:setPlayerMark(player, "@rfenghou_3d13j__wanzhen", 0)
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d13j__wanzhen"] = "完阵",
  [":rfenghou_3d13j__wanzhen"] = "当你需要使用或打出基本牌时，你可以横置，视为使用或打出之，或重置并弃置3-X张牌（X为你因此横置期间使用的牌数且至多为3）。",

  ["@rfenghou_3d13j__wanzhen"] = "完阵弃牌",
  ["#rfenghou_3d13j__wanzhen_trigger"] = "完阵",
  ["#rfenghou_3d13j__wanzhen-viewas1"] = "完阵：你可横置武将牌，视为使用或打出基本牌（能以此法转化）",
  ["#rfenghou_3d13j__wanzhen-viewas2"] = "完阵：你可重置武将牌，然后弃置 %arg 张牌（以此法转化牌作废）",
}

return wanzhen
